using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace miciv
{
    public class ComponentPrototypeBase<T> : IComponentPrototype where T : class, IComponentManager
    {
        protected string m_strID;
        protected EntityPrototype m_enProto;
        protected T m_coManager;

        public ComponentPrototypeBase()
        {
        }

        #region IComponentPrototype Membres

        virtual public string ID
        {
            get
            {
                return this.m_strID;
            }
        }

        virtual public EntityPrototype Entity
        {
            get
            {
                return this.m_enProto;
            }
        }

        virtual public IComponentManager Manager
        {
            get
            {
                return this.m_coManager;
            }
        }

        virtual public CoreContext CoreContext
        {
            get
            {
                return this.Manager.CoreContext;
            }
        }

        virtual public CoreManager CoreManager
        {
            get
            {
                return this.Manager.CoreManager;
            }
        }

        virtual public bool Initialize(EntityPrototype _enProto, XmlElement _xmlConfig, IComponentManager _coManager)
        {
            this.m_enProto = _enProto;
            this.m_strID = _xmlConfig.Attributes["id"].Value;
            this.m_coManager = _coManager as T;
            return true;
        }

        virtual public void Release()
        {

        }

        #endregion
    }

    public class ComponentInstanceBase<T> : IComponentInstance where T : class, IComponentPrototype
    {
        protected Entity m_entity;
        protected T m_coProto;
        protected string m_strID;

        public ComponentInstanceBase()
        {
        }

        #region IComponentInstance Membres

        virtual public string ID
        {
            get
            {
                return this.m_strID;
            }
        }

        virtual public Entity Entity
        {
            get
            {
                return this.m_entity;
            }
        }

        virtual public IComponentPrototype Prototype
        {
            get
            {
                return this.m_coProto;
            }
        }

        virtual public CoreContext CoreContext
        {
            get
            {
                return this.m_coProto.CoreContext;
            }
        }

        virtual public CoreManager CoreManager
        {
            get
            {
                return this.m_coProto.CoreManager;
            }
        }

        virtual public bool Initialize(Entity _entity, IComponentPrototype _coProto)
        {
            this.m_entity = _entity;
            this.m_coProto = _coProto as T;
            this.m_strID = string.Format("{0}.{1}.", this.Entity.ID, this.Prototype.ID);
            return true;
        }

        virtual public void PostInitialize(XmlElement _xmlComponent)
        {
            this.m_strID = string.Format("{0}.{1}.", this.Entity.ID, this.Prototype.ID);
        }

        virtual public void Update(float _deltaTime)
        {

        }

        virtual public void Release()
        {

        }

        #endregion
    }
}
